<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置<small>角色管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户编号/用户姓名" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                <el-button @click="search()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>

            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="id" label="用户编号" align="center"></el-table-column>
                <el-table-column prop="name" label="用户姓名" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                   <!--     <el-button type="primary" size="mini" @click="allocationMenu(scope.row)">分配菜单</el-button>-->
                       <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>-->
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                    <el-dialog  title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                        <template>
                            <el-tabs v-model="activeName" type="card">
                                <el-tab-pane label="基本信息" name="first">
                                    <el-form label-position="right" label-width="100px">
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="角色姓名">
                                                    <el-input v-model="formData.name"/>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="12">
                                                <el-form-item label="角色关键字">
                                                    <el-input v-model="formData.keyword"/>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                        <el-row>
                                            <el-col :span="24">
                                                <el-form-item label="角色描述">
                                                    <el-input v-model="formData.description" type="textarea"></el-input>
                                                </el-form-item>
                                            </el-col>
                                        </el-row>
                                    </el-form>
                                </el-tab-pane>
                                <el-tab-pane label="权限信息" name="second">
                                    <div class="checkScrol">
                                        <table class="datatable">
                                            <thead>
                                            <tr>
                                                <th>选择</th>
                                                <th>权限名称</th>
                                                <th>权限关键字</th>
                                                <th>权限说明</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <tr v-for="p in tableData">
                                                <td>
                                                    <input :id="p.id" v-model="permissionIds" type="checkbox" :value="p.id">
                                                </td>
                                                <td><label :for="p.id">{{p.name}}</label></td>
                                                <td><label :for="p.id">{{p.keyword}}</label></td>
                                                <td><label :for="p.id">{{p.description}}</label></td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </div>
                                </el-tab-pane>
                                <el-tab-pane label="菜单信息" name="third">
                                    <el-tree
                                            :data="menuData"
                                            show-checkbox
                                            node-key="id"
                                            default-expand-all
                                            :expand-on-click-node="false">
                                      <span class="custom-tree-node" slot-scope="{ node, data }">
                                        <span>{{ data.name }}</span>

                                      </span>
                                    </el-tree>
                                </el-tab-pane>
                            </el-tabs>
                        </template>

                        <div slot="footer" class="dialog-footer">
                            <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                            <el-button type="primary" @click="handleEdit()">确定</el-button>
                        </div>
                    </el-dialog>
                </el-dialog>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog @opened="setMenuSelect" title="新建角色" :visible.sync="dialogFormVisible">
                <el-dialog  title="新建角色" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="角色姓名">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="角色关键字">
                                                <el-input v-model="formData.keyword"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="角色描述">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="权限信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>权限名称</th>
                                            <th>权限关键字</th>
                                            <th>权限说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="p in tableData">
                                            <td>
                                                <input :id="p.id" v-model="permissionIds" type="checkbox" :value="p.id">
                                            </td>
                                            <td><label :for="p.id">{{p.name}}</label></td>
                                            <td><label :for="p.id">{{p.keyword}}</label></td>
                                            <td><label :for="p.id">{{p.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                            <el-tab-pane label="菜单信息" name="third">
                                <el-tree
                                        :data="menuData"
                                        show-checkbox
                                        node-key="id"
                                        default-expand-all
                                        :expand-on-click-node="false">
                                      <span class="custom-tree-node" slot-scope="{ node, data }">
                                        <span>{{ data.name }}</span>

                                      </span>
                                </el-tree>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
                </el-dialog>
            </div>



        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize:10,//每页显示的记录数
                total:0,//总记录数
                queryString:null,//查询条件

            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            activeName:'first',//添加/编辑窗口Tab标签名称
            menuData:[],
            currentRole:'',
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit:false,//编辑表单是否可见
            tableData:[],//添加表单窗口中权限列表数据
            menuIds:[],
            permissionIds:[],
            defaultCheckedKeys:[],//tree默认选中数据
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        mounted(){
        },
        methods: {
            //条件查询
            search() {
                this.pagination.currentPage=1;
                this.findPage();
            },
            setMenuSelect(){ //在对话框打开时设置用户菜单选中
                // var treeObj = this.$refs['tree'];
                // treeObj.setCheckedKeys(this.menuIds)
            },
            //添加
            handleAdd () {
                //点击提交的时候发送post请求，提交的数据据现在有角色的基本信息，用户勾选的角色信息，用户勾选的菜单信息
                //现在三个参数合并到一起提交
                this.formData.permissionIds = this.permissiondIds;//就需要在Role加一个属性

                axios.post("/role/add.do",this.formData).then((response)=>{
                    if (response.data.flag) {
                        //隐藏新增窗口
                        this.dialogFormVisible=false;
                        //刷新页面加载数据
                        this.findPage();
                    }
                    //显示给用户提示信息（不管成功失败都要提示）
                    this.$message({
                        message:response.data.message,
                        type:response.data.flag?'success':'error'
                    })

                })



           /*      //获取树形菜单选中的值
                let menus = this.$refs['tree'].getCheckedKeys();
                axios.post("/role/addRoleMenu.do?roleId=" + this.currentRole + "&menus=" + menus).then((res)=>{
                    this.dialogFormVisible = false;
                    this.$message({
                        message:res.data.message,
                        type:res.data.flag ? "success" : "error"
                    })
                })*/

            },
            //分页查询
            findPage() {
                //当前页
                //每页显示条数
                //查询条件
                var queryParam = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                }
                axios.post("/role/findPage.do", queryParam).then((res) => {
                    console.log(res);
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                })
            },
            // 重置表单
            resetForm() {

                this.formData = {}
            },
            // 弹出添加窗口
            handleCreate() {

                //    弹出新增框之前先清空表格数据
                this.resetForm();
                //1.查询出所有的权限信息


                axios.get("/permission/findAll.do").then((res)=>{
                    this.tableData = res.data.data.permissions;

                    this.menuData= res.data.data.menus;
                    //2。弹出新增弹窗
                    this.dialogFormVisible = true;

                })



            },
            //分配菜单
            allocationMenu(row){
           /*     axios.get("/menu/getMenus.do?id=0&roleId=" + row.id).then((res)=>{
                    this.menuData = res.data.data.menus;//所有的菜单
                    this.currentRole = row.id;
                    this.dialogFormVisible = true;
                    // this.menuIds =
                    this.defaultCheckedKeys = res.data.data.menuIds;//用户拥有的菜单id
                })*/
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                //查询当前检查项详情
              /*  axios.get("/checkitem/findById.do?id=" +row.id).then((res)=>{
                    this.formData = res.data.data
                    this.dialogFormVisible4Edit = true;
                })*/
                //根据id查询，回显数据
               axios.get("/role/findById4Edit.do?id="+row.id).then((res)=>{
                   console.log(res.data.data.permissionIds);
                   this.formData = res.data.data.role;
                   this.tableData = res.data.data.permissions;
                   this.permissionIds = res.data.data.permissionIds;

                    //显示编辑框
                    this.dialogFormVisible4Edit = true;
                })
                //显示修改框s
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();



                // axios.post("",).then(()=>{})
                // axios.get("").then(()=>{})

            },
            sleep(delay) {
                var start = (new Date()).getTime();
                while((new Date()).getTime() - start < delay) {
                    continue;
                }
            }

        }
    })
</script>
</html>
